package tij4.concurrent.exercise.ex26;

import java.sql.Time;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/*
    waitPerson接受客人的订单后唤醒chef
    chef被唤醒后按照订单做菜
    chef做完后交由waitPersion上菜
    客人吃完后waitPersion通知busboy收拾餐桌
    busboy按照waitPerson通知收拾餐桌
    busboy收拾完后通知waitPersion可以接待下一桌客人
    以后就是周而复始的循环了...
 */
class Meal {
    private final int orderNum;

    public Meal(int orderNum) {
        this.orderNum = orderNum;
    }

    @Override
    public String toString() {
        return "Meal" + orderNum;
    }
}

class DiningTable {
    private boolean clean = true;
    private final int tableNum;

    public DiningTable(int tableNum) {
        this.tableNum = tableNum;
    }

    public boolean isClean() {
        return clean;
    }
    public void setClean(boolean clean) {
        this.clean = clean;
    }

    public int getTableNum() {
        return tableNum;
    }
}

class WaitPersion implements Runnable {
    private Restaurant restaurant;
    public int count = 0;

    public WaitPersion(Restaurant restaurant) {
        this.restaurant = restaurant;
    }


    @Override
    public void run() {
        try{
            while (!Thread.interrupted()) {
                synchronized (this) {
                    while (restaurant.meal == null) {
                        wait();
                    }
                }
                System.out.println("Waitpersion got " + restaurant.meal);
                synchronized (restaurant.chef) {
                    restaurant.meal = null;
                    restaurant.chef.notifyAll();
                    restaurant.diningTable = new DiningTable(++count);
                }

                synchronized (this) {
                    while (restaurant.diningTable == null || !restaurant.diningTable.isClean()) {
                        wait();
                    }
                }
                synchronized (restaurant.busBoy) {
                    restaurant.diningTable.setClean(false);
                    restaurant.busBoy.notifyAll();
                }
            }

        }catch (InterruptedException e) {
            System.out.println("WaitPersion interrupted");
        }
    }
}

class Chef implements Runnable {
    private Restaurant restaurant;
    private int count = 0;

    public Chef(Restaurant restaurant) {
        this.restaurant = restaurant;
    }

    @Override
    public void run() {
        try {
            while (!Thread.interrupted()) {
                synchronized (this) {
                    while (restaurant.meal != null)
                        wait();
                }
                if (++count == 10) {
                    System.out.println("Out of food, closing");
                    restaurant.exec.shutdownNow();
                    return;
                }
                System.out.println("Order up!");
                synchronized (restaurant.waitPersion) {
                    restaurant.meal = new Meal(count);
                    restaurant.waitPersion.notifyAll();
                }
                TimeUnit.MILLISECONDS.sleep(100);
            }
        }catch (InterruptedException e) {
            System.out.println("Chef interrrupted");
        }
    }
}

class BusBoy implements Runnable {
    private Restaurant restaurant;

    public BusBoy(Restaurant restaurant) {
        this.restaurant = restaurant;
    }

    /*

        拿到自己的锁就表示开始叫自己呢,要干活
        waitPersion拿到meal我们才能干
        收拾食物
        打扫完了通知waitPerson
     */
    @Override
    public void run() {
        try {
           while (!Thread.interrupted()) {
               synchronized (this) {
                   while (restaurant.diningTable == null || restaurant.diningTable.isClean()) {
                       wait();
                   }
               }
               System.out.println("BusBoy got " + restaurant.diningTable.getTableNum());
               synchronized (restaurant.waitPersion) {
                   restaurant.diningTable.setClean(true);
                   restaurant.waitPersion.notifyAll();
               }
               TimeUnit.MILLISECONDS.sleep(100);
           }
        }catch (InterruptedException e) {
            System.out.println("BusBoy interrupted");
        }

    }
}

public class Restaurant {
    Meal meal;
    DiningTable diningTable;
    ExecutorService exec = Executors.newCachedThreadPool();
    WaitPersion waitPersion = new WaitPersion(this);
    Chef chef = new Chef(this);
    BusBoy busBoy = new BusBoy(this);

    public Restaurant() {
        exec.execute(waitPersion);
        exec.execute(chef);
        exec.execute(busBoy);
    }

    public static void main(String[] args) {
        new Restaurant();
    }
}
